package com.example.liaotian.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;

import com.example.liaotian.Fragment.MyContent.MyActFragment;
import com.example.liaotian.Fragment.MyContent.MyTalkFragment;
import com.example.liaotian.Fragment.MyContent.MyTraFragment;
import com.example.liaotian.Fragment.UpFragment.ActFragment;
import com.example.liaotian.Fragment.UpFragment.TalkFragment;
import com.example.liaotian.Fragment.UpFragment.TransactionFragment;
import com.example.liaotian.R;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class MyContentActivity extends AppCompatActivity {
    private List<Fragment> fragments;
    private ArrayList<String> title;
    private TabLayout tabLayout;
    private ViewPager viewPager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_content);

        initView();
    }

    private void initView() {
        tabLayout = findViewById(R.id.table);
        viewPager = findViewById(R.id.viewpage);

        fragments = new ArrayList<>();
        title = new ArrayList<>();

        title.add("社区讨论");
        title.add("组织活动");
        title.add("二手交易");

        fragments.add(new MyTalkFragment());
        fragments.add(new MyActFragment());
        fragments.add(new MyTraFragment());

        /**
         * 这里用了匿名对象的方式来创建适配器
         *
         */
        viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            /**
             * 把每个fragment填入每个pager
             * @param position
             * @return
             */
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            /**
             * pager的长度就是fragment的数量
             * @return
             */
            @Override
            public int getCount() {
                return fragments.size();
            }

            /**
             * 给tablayout获取标题
             * @param position
             * @return
             */
            @Override
            public CharSequence getPageTitle(int position) {
                return title.get(position);
            }
        });
        /**
         *把pager和tablayout绑定
         */
        tabLayout.setupWithViewPager(viewPager);
    }
    }

